#include <sys_head.h>

int x = 0;

int main(int argc, char const *argv[])
{
    int fd[2];
    if (-1 == pipe(fd))
    {
        printf("创建无名管道失败\n");
    }
    else
    {
        printf("fd[0]=%d,fd[1]=%d\n",fd[0],fd[1]);
    }
    pid_t pid = fork();
    while (1)
    {
        if (0 == pid)
        {
            char buf[128];
            read(fd[0],buf,sizeof(buf));
            x = atoi(buf);
            x++;
            printf("子进程读取的内容为:%d\n",x);
            sprintf(buf,"%d",x);
            write(fd[1],buf,sizeof(buf));
            sleep(1);
        }
        else if (0 < pid)
        {
            char buf[128];
            sprintf(buf,"%d",x);
            write(fd[1],buf,sizeof(buf));
            sleep(1);
            read(fd[0],buf,sizeof(buf));
            x = atoi(buf);
            x++;
            printf("主进程读取的内容为:%d\n",x);
        }
        sleep(1);
    }
    
    return 0;
}
